package com.example.anydo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.anydo.R;
import com.example.anydo.bean.SubTaskBean;
import com.example.anydo.bean.TaskBean;
import com.example.anydo.util.database.DatabaseHelper;
import com.example.anydo.util.pub_util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class SubTaskAdapter extends RecyclerView.Adapter<SubTaskAdapter.ViewHolder> {
    private List<SubTaskBean.DataBean> dataBeanList;
    private Context context;
    private DatabaseHelper helper;

    public SubTaskAdapter(ArrayList<SubTaskBean.DataBean> dataBeanList, Context context){
        this.dataBeanList = dataBeanList;
        this.context = context;
        helper = new DatabaseHelper(context);
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.subtask_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull final ViewHolder holder, int position) {
        final SubTaskBean.DataBean dataBean = dataBeanList.get(position);
        if(dataBean.getIfDone() != null){
            holder.checkBoxIsFinished.setChecked(dataBean.getIfDone().equals("是"));
            holder.checkBoxIsFinished.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                    holder.textViewTitle.setTextColor(b ? context.getResources().getColor(R.color.green) :
                            context.getResources().getColor(R.color.black));

                    // 数据缓存
                    helper.setSubTaskState(dataBean.getId(), Integer.parseInt(SPUtil.getUserId(context)),b);
                }
            });
        }
        if(dataBean.getTitle() != null){
            holder.textViewTitle.setTextColor(holder.checkBoxIsFinished.isChecked() ? context.getResources().getColor(R.color.green) : context.getResources().getColor(R.color.black));
            holder.textViewTitle.setText(dataBean.getTitle());
        }
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        CheckBox checkBoxIsFinished;
        TextView textViewTitle;
        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            this.checkBoxIsFinished = itemView.findViewById(R.id.checkbox_finished);
            this.textViewTitle = itemView.findViewById(R.id.txt_subtask_msg);
        }
    }
}
